#### 一致性聚类 ####
library(tidyverse)
library(ConsensusClusterPlus)

try({
  getwd()
  setwd('./一致性聚类/')
})


try({
  setwd('父级路径')
})

exp <- read.table("O2_fpkm.txt",sep = "\t",row.names = 1,check.names = F,stringsAsFactors = F,header = T)

gene <- c("LDHA","HIF1A","PFKP","PFKL") 
d <- exp[gene,]
d=as.matrix(d)
mads=apply(d,1,mad)
d=d[rev(order(mads))[1:4],] #5是基因个数 
d = sweep(d,1, apply(d,1,median,na.rm=T))

title=(paste0(getwd(),"/")) ##文件夹输出图片的位置
set.seed(1) #我发现设不设置种子都一样
results = ConsensusClusterPlus(d,
                               maxK=9,
                               reps=50,
                               pItem=0.8,
                               pFeature=1,
                               title=title,
                               clusterAlg="hc",
                               distance="pearson",
                               seed=1,
                               plot="png"
)
results = ConsensusClusterPlus(d,
                               maxK=9,
                               reps=50,
                               pItem=0.8,
                               pFeature=1,
                               title=title,
                               clusterAlg="hc",
                               distance="pearson",
                               seed=1,
                               plot="pdf"
)
# results[[2]][["consensusMatrix"]][1:5,1:5]
# results[[2]][["consensusTree"]]
# results[[2]][["consensusClass"]][1:5]
icl = calcICL(results,title=title,plot="pdf") ##画另一组图片
icl = calcICL(results,title=title,plot="png") ##画另一组图片

# group<-results[[3]][["consensusClass"]]
# group<-as.data.frame(group)
# group$group <- factor(group$group,levels=c(1,2))
# save(group,file = "group_AY.Rda")
